"""
Case Type   : GUC
Case Name   : 使用gs_guc set方法设置参数extra_float_digits为3 ,观察预期结果
Create At   : 2020/12/2
Owner       : opentestcase005
Description :
        1.查询extra_float_digits默认值
        2.修改参数值为3；建表查询
        3.恢复参数默认值
Expect      :
        1.显示默认值为0
        2.设置成功;建表且数据插入成功
        3.默认值恢复成功
History     :
        2024/12/28:openGauss 7.0.0-RC1版本合入float性能优化需求，导致期望结果变更，所以修改用例。相关问题见openGauss-server仓的IBE9VW
        2025/1/20 modified by li-xin12345：回显变化，优化用例
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

LOG = Logger()
commonsh = CommonSH('dbuser')


class ClientConnection(unittest.TestCase):
    def setUp(self):
        LOG.info(
            '---Opengauss_Function_Guc_ClientConnection_Case0141start---')
        self.constant = Constant()

    def test_extra_float_digits(self):
        LOG.info('--步骤1:查看默认值--')
        sql_cmd = commonsh.execut_db_sql('''show extra_float_digits;''')
        LOG.info(sql_cmd)
        self.res = sql_cmd.splitlines()[-2].strip()
        LOG.info('--步骤2:设置参数值为3并重启数据库--')
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     'extra_float_digits = 3')
        LOG.info(msg)
        self.assertTrue(msg)
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info('--步骤3:查询参数值并建表插入FLOAT4类型数据--')
        sql_cmd = commonsh.execut_db_sql('''show extra_float_digits;
            drop table if exists float_type_t3;
            create table float_type_t3 (FT_COL2 FLOAT4);
            insert into float_type_t3 values(10.365456);
            select * from float_type_t3;
            ''')
        LOG.info(sql_cmd)
        self.assertIn('3', sql_cmd)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_cmd)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_cmd)
        self.assertIn('10.365456', sql_cmd)

    def tearDown(self):
        LOG.info('--步骤4:恢复默认值--')
        sql_cmd = commonsh.execut_db_sql('drop table if exists float_type_t3;')
        LOG.info(sql_cmd)
        sql_cmd = commonsh.execut_db_sql('''show extra_float_digits;''')
        LOG.info(sql_cmd)
        msg = commonsh.execute_gsguc('set',
                                     self.constant.GSGUC_SUCCESS_MSG,
                                     f'extra_float_digits={self.res}')
        LOG.info(msg)
        msg = commonsh.restart_db_cluster()
        LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        sql_cmd = commonsh.execut_db_sql('''show extra_float_digits;''')
        LOG.info(sql_cmd)
        LOG.info(
            '--Opengauss_Function_Guc_ClientConnection_Case0141执行完成----')
